package com.mayisoftware.mayioa.platform.impl.serviceImpl;

import com.mayisoftware.mayioa.common.impl.utils.StringUtils;
import com.mayisoftware.mayioa.platform.api.domain.SysMail;
import com.mayisoftware.mayioa.platform.api.service.ISysConfigService;
import com.mayisoftware.mayioa.platform.api.service.ISysMailService;
import com.mayisoftware.mayioa.platform.impl.dao.SysMailInfoMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Properties;

/**
 * 邮件服务
 */
@Service
public class SysMailService implements ISysMailService {
    private static final Logger log = LoggerFactory.getLogger(SysMailService.class);
    /**
     * 系统参数服务
     */
    @Autowired
    ISysConfigService sysConfigService;

    @Autowired
    SysMailInfoMapper sysMailInfoMapper;


    /**
     * 发送邮箱类型 暂时只支持qq邮箱
     */
    final String sys_mail_type = "sys.mail.type";
    public String getSys_mail_type(){
        return sysConfigService.selectConfigByKey(sys_mail_type);
    }

    /**
     * 发送邮箱密码
     */
    final String sys_mail_password = "sys.mail.password";
    public String getSys_mail_password(){
        return sysConfigService.selectConfigByKey(sys_mail_password);
    }

    /**
     * 发送邮箱用户名
     */
    final String sys_mail_username = "sys.mail.username";
    public String getSys_mail_username(){
        return sysConfigService.selectConfigByKey(sys_mail_username);
    }

    /**
     * 获取邮箱消息
     * @return
     */
    public SimpleMailMessage getMessage(String sendTo, String sendFrom, String subject, String content){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(sendTo);
        message.setFrom(sendFrom);
        message.setSubject(subject);
        message.setText(content);
        return message;
    }

    /**
     * 获取qq邮箱JavaMailSender
     * @return
     */
    public JavaMailSender getQQMailSender(String userName, String password){
        if(StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)){
            log.error("参数为null");
            return null;
        }
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
//        javaMailSender.setUsername("56261056@qq.com");
        javaMailSender.setUsername(userName);
//        javaMailSender.setPassword("saezjdkrbzyscabg");
        javaMailSender.setPassword(password);
        javaMailSender.setHost("smtp.qq.com");
        javaMailSender.setDefaultEncoding("UTF-8");
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.auth","true");
        properties.setProperty("mail.smtp.auth.starttls.enable","true");
        properties.setProperty("mail.smtp.auth.starttls.required","true");
        return javaMailSender;
    }

    /**
     * 获取ipsa邮箱JavaMailSender
     * @return
     */
    public JavaMailSender getIPSAMailSender(String userName, String password){
        if(StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)){
            log.error("参数为null");
            return null;
        }
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
//        javaMailSender.setUsername("56261056@qq.com");
        javaMailSender.setUsername(userName);
//        javaMailSender.setPassword("saezjdkrbzyscabg");
        javaMailSender.setPassword(password);
        javaMailSender.setHost("smtp.mayisoftware.com");
        javaMailSender.setDefaultEncoding("UTF-8");
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.auth","true");
        properties.setProperty("mail.smtp.auth.starttls.enable","true");
        properties.setProperty("mail.smtp.auth.starttls.required","true");
        return javaMailSender;
    }



    /**
     * 发送简单邮件
     */
    @Override
    public void sendSimpleEmail(String sendTo, String subject, String content){
        if(StringUtils.isEmpty(sendTo) || StringUtils.isEmpty(subject) || StringUtils.isEmpty(content)) {
            log.error("邮件发送失败,参数为空.");
            return;
        }
        SimpleMailMessage message = getMessage(sendTo, getSys_mail_username(), subject, content);
        JavaMailSender mailSender = null;
        if ("qq".equals(getSys_mail_type())){
            mailSender = getQQMailSender(getSys_mail_username(), getSys_mail_password());
        }
        else if("ipsa".equals(getSys_mail_type())){
            mailSender = getIPSAMailSender(getSys_mail_username(), getSys_mail_password());
        }
        if(mailSender == null){
            log.error("无法实例化JavaMailSender");
            return;
        }
        mailSender.send(message);
    }

    /**
     * 邮件入库
     */
    @Override
    public int insertMail(String mailReceiver, String mailTitle, String mailContent){
        if(StringUtils.isEmpty(mailReceiver) || StringUtils.isEmpty(mailTitle) || StringUtils.isEmpty(mailContent)){
            log.error("参数为空");
            return 0;
        }
        SysMail sysMail = new SysMail();
        sysMail.setMailReceiver(mailReceiver);
        sysMail.setMailTitle(mailTitle);
        sysMail.setMailContent(mailContent);
        sysMail.setMailState(0);
        int rowNum = sysMailInfoMapper.insertMail(sysMail);
        if(rowNum == 1){
            log.info("插入邮件表.");
        }else{
            log.error("插入邮件表失败");
        }
        return rowNum;
    }

    /**
     * 查询未发送/发送失败的邮件列表
     */
    @Override
    public List<SysMail> selectUnsendMailList(){
        return sysMailInfoMapper.selectMailListByState(0);
    }

    /**
     * 更新邮件列表为已发送
     */
    @Override
    public int updateMailToSend(SysMail mail){
        mail.setMailState(1);
        return sysMailInfoMapper.updateMail(mail);
    }
}
